'''
@Descripttion: 
@version: 
@encoding: utf-8
@Author: qiurongcan
Date: 2025-08-31 14:33:58
LastEditTime: 2025-08-31 14:42:24
'''

from collections import OrderedDict

class LRUCache:

    def __init__(self, capacity):
        
        self.capacity = capacity

        self.cache = OrderedDict()



    def get(self, key):

        if key not in self.cache:
            return -1
        
        self.cache.move_to_end(key, last=False)

        return self.cache[key]

    
    def put(self, key, value):

        self.cache[key] = value
        # 将这个元素移动到链表头部
        self.cache.move_to_end(key, last=False)

        if len(self.cache) > self.capacity:

            self.cache.popitem() # 去掉最后一个元素

